package com.lzy.cloud.permission.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.permission.dtos.RoleDto;
import com.lzy.cloud.permission.facade.RoleFacade;
import com.lzy.cloud.permission.request.RoleRequest;
import com.lzy.cloud.permission.response.RoleResponse;
import com.lzy.cloud.permission.service.RoleService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by WIN7 on 2018/5/16.
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    RoleFacade roleFacade;

    /**
     * 新增角色
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public Result addRole(@RequestBody(required = false) RoleRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = roleService.insertSelective(DozerUtils.map(request, RoleDto.class));
        Ensure.that(ok).isTrue("00020001");
        return new Result(ok);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{id}")
    public Result delRole(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010001");
        Boolean ok = roleFacade.delete(id);
        Ensure.that(ok).isTrue("00020002");
        return new Result(ok);
    }

    /**
     * 编辑角色
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update")
    public Result updateRole(@RequestBody RoleRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = roleService.update(DozerUtils.map(request, RoleDto.class));
        Ensure.that(ok).isTrue("00020003");
        return new Result(ok);
    }

    /**
     * 分页查询角色列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public Result selectList(@RequestBody RoleRequest request) {
        Ensure.that(request).isNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<RoleDto> pageInfo = roleService.selectRole(pager, DozerUtils.map(request, RoleDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), RoleResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 角色详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}")
    public Result selectById(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010001");
        RoleDto roleDto = roleService.selectById(id);
        Ensure.that(roleDto).isNotNull("00020004");
        return new Result(DozerUtils.map(roleDto, RoleResponse.class));
    }
}
